package rpc

import (
	"fmt"

	"gitee.com/go-course/go11/devcloud-mini/mcenter/apps/endpoint"
	"gitee.com/go-course/go11/devcloud-mini/mcenter/apps/token"
	"gitee.com/go-course/go11/devcloud-mini/mcenter/conf"
	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func NewClient(c conf.Grpc) *Client {
	fmt.Println(c.Address())
	conn, err := grpc.Dial(
		c.Address(),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		// 补充GRPC 客户端 Trace中间件
		grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
		grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
	)
	if err != nil {
		panic(err)
	}
	return &Client{
		c: conn,
	}
}

type Client struct {
	c *grpc.ClientConn
}

// token 模块的rpc服务
func (c *Client) Token() token.RpcClient {
	return token.NewRpcClient(c.c)
}

// endpoint 模块的rpc服务
func (c *Client) Endpoint() endpoint.RpcClient {
	return endpoint.NewRpcClient(c.c)
}
